//
//  SEGDoorBaseController.m
//  SEGUhomeIntelligentHardware
//
//  Created by MOON FLOWER on 2018/7/6.
//  Copyright © 2018年 刘山. All rights reserved.
//

//Controller
#import "SEGDoorBaseController.h"
#import "SEGOpenDoorProblemViewController.h"
#import "SEGOpenDoorNoticeChooseViewController.h"
#import "SEGKnowOpenDoorViewController.h"
#import "SEGOpenDoorStrategyViewController.h"
//Model
#import "SEGOpenDoorBussinessManager.h"
//View
#import "SEGHAllDoorView.h"
#import "SEGOpenDoorAlertView.h"
//ViewModel
#import <SEGViewModel/SEGDoorListViewModel.h>

@interface SEGDoorBaseController () <SEGNewAlertViewDelegate>

@property (nonatomic, assign) BOOL isFromServerList; // 是否是从服务页面进入
@property (nonatomic, assign) SEGOpenDoorStatus doorStatus;
@property (nonatomic, copy) void (^completedCallback)(SEGNewOpenDoorModel *);
@property (nonatomic,assign) SEGEnumOpendoorShowType type;
@property (nonatomic, strong) SEGDoorListViewModel *doorListViewModel;

@end

@implementation SEGDoorBaseController

/**
 开门初始化

 @param type 门类型
 @param isFromServerList 是否是从服务页进入
 @param completed 操作门回调
 @return self
 */
- (instancetype)initWithDoorType:(SEGEnumOpendoorShowType)type isFromServerList:(BOOL)isFromServerList completed:(void(^)(SEGNewOpenDoorModel*))completed {
    if (self == [super init]) {
        _type = type;
        _isFromServerList = isFromServerList;
        _completedCallback = completed;
    }
    return self;
}

- (void)dismissViewController {
    [self.navigationController dismissViewControllerAnimated:YES completion:^{
        if (self.disapperCallback) {
            self.disapperCallback();
        }
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kColor_W;
    self.navDisplayType = SEGNavigationBarDisplayTypeHide;
    
    // !!!:  Wendy，2018.1.8, 提交开门记录(每次进入开门界面都要提交，如代码逻辑改动必须随逻辑改动！！！)
    [self.doorListViewModel submitOpenDoorHistory];
//    [self.doorManager submitOpenDoorHistory];
    
    // 另外两种类型使用SEGDoorNoCooperationOrCertificationView展示
    [self allDoorView];
}

- (void)allDoorView {
    self.title = @"开门";
    SEGHAllDoorView *doorView = [[SEGHAllDoorView alloc]initWithFrame:CGRectMake(0, 0, LYScreenWidth(), LYScreenHeight())];
    if (self.isFromServerList) {
        [doorView updateTipLabelText:@"长按门禁按钮，拖拽调整排序\n前8个着色按钮将显示在首页"];
    }
    [doorView setCloseBox:^{
        [self dismissViewController];
    }];
    
    //点击问题
    [doorView  setProblemBlock:^{
        SEGOpenDoorNoticeChooseViewController *settingVC = [SEGOpenDoorNoticeChooseViewController new];
        settingVC.isHideBar=YES;
        [self.navigationController pushViewController:settingVC animated:YES];
    }];
    
    [doorView setItemBox:^(SEGNewMyDoorListModel *model) {
        [self openDoor:model];
    }];
    
    [self.view addSubview:doorView];
    [doorView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    // 在请求完成之前 先展示本地数据
    [doorView setDataSource:[self.doorListViewModel getLocalAllDoorList]];
    // 请求网络门禁列表数据
    [[self.doorListViewModel requestNetAllDoorList] subscribeNext:^(NSArray<SEGNewMyDoorListModel *> * newDoorList) {
        [doorView needUpdateData:newDoorList];
    } error:^(NSError * _Nullable error) {
        [SEGAlertView showTmepMessageWithMessage:error.localizedDescription withtime:0.1];
    }];
}

- (void)seg_doorResponseWithCode:(NSString*)code data:(SEGNewMyDoorListModel*)data {
    //关闭
    if ([code isEqualToString:@"CLOSE"]) {
        self.presentingViewController.tabBarController.tabBar.hidden = NO;
        [self dismissViewController];
        return;
    }
    //了解一键开门
    if ([code isEqualToString:@"KNOWMORE"]) {
        SEGKnowOpenDoorViewController *knowOpenDoor = [SEGKnowOpenDoorViewController new];
        knowOpenDoor.isHideBar=YES;
        [self.navigationController pushViewController:knowOpenDoor animated:YES];
        return;
    }
    // 未认证用户
    if ([code isEqualToString:@"AUTHENTICATION"]) {
        //用原来的界面
        return;
    }
    //  常用门中-开门失败展示攻略
    if ([code isEqualToString:@"DOORSTRATEGY"]) {
        SEGOpenDoorStrategyViewController *VC = [SEGOpenDoorStrategyViewController new];
        VC.isHideBar=YES;
        [self.navigationController pushViewController:VC animated:YES];
        return;
    }
}

-(void)openDoor:(SEGNewMyDoorListModel*)data {
    if (!data) {
        return;
    }
    
    [SEGOpenDoorBussinessManager shareInstance].openSuccessBlock = ^{
        [self dismissViewController];
    };
    
    [[SEGOpenDoorBussinessManager shareInstance] openDoorWithDoorModel:data completeCallback:^(SEGNewOpenDoorModel * _Nonnull data) {
        if (self.completedCallback) {
            self.completedCallback(data);
        }
    }];
}

#pragma mark - Getter
- (SEGDoorListViewModel *)doorListViewModel {
    if (!_doorListViewModel) {
        _doorListViewModel = [SEGDoorListViewModel new];
    }
    return _doorListViewModel;
}

@end
